package com.bdk.middle.management.service.impl;

import com.bdk.middle.management.api.domain.AdminDO;
import com.bdk.middle.management.service.RoleService;
import com.bdk.middle.management.service.AdminService;
import com.bdk.middle.common.result.ResultDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class AdminUserDetailsService implements UserDetailsService {

    private Logger          logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.debug("用户名：" + username);
        ResultDO result = adminService.getUserByName(username);
        if(!result.isSuccess()) {
            logger.debug(result.getErrorCode().getMsg());
            throw new UsernameNotFoundException(result.getErrorCode().getMsg());
        }
        AdminDO domain = (AdminDO) result.getModel(ResultDO.FIRST_MODEL_KEY);
        return domain;
    }

}
